﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Algoritmia.DataStructures.Queues
{
    public class RingBufferLifo<T> : ILifo<T>
    {
        private int _last,  _count, _capacity;
        private T[] _array;


        public RingBufferLifo(IEnumerable<T> items = null, int capacity=0)
        {
            
            if (items != null)
            {
                _array = items.ToArray();
                _last = _array.Length - 1;
                _count = _array.Length;
                if (_array.Length < capacity)
                {
                    _capacity = capacity;
                    Array.Resize(ref _array,_capacity);
                }
                else
                {
                    _capacity = _array.Length;
                }
            }
            else
            {
                _count = 0;
                _last = 0;
                _capacity = capacity > 0
                                ? capacity
                                : 1;
                _array = new T[_capacity];
            }
        }

        private void DoubleCapacity()
        {
            _capacity *= 2;
            Array.Resize(ref _array,_capacity);
        }

        #region IQueue<T> Members

        public void Push(T elem)
        {
            if ( _count == _capacity)
            {
                DoubleCapacity();
            }

            _last = (_last + 1) % _capacity;
            _array[_last] = elem;
            _count++;
        }


        public T Pop()
        {
            if ( _count > 0)
            {
                T retValue = _array[_last];
                _last = (_last - 1) %_capacity;
                _count--;
                return retValue;
            }
            throw new InvalidOperationException("Is not possible to pop an element from an empty Lifo.");
        }

        public T Top()
        {
            if (_count > 0)
            {
                return _array[_last];
            }
            throw new InvalidOperationException("Is not possible to peek the top element from an empty Lifo.");
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            int i = _last;
            int finish = (_last - _count)%_capacity;
            while ( i != finish)
            {
                yield return _array[i];
                i = (i - 1)%_capacity;
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region ICountable Members

        public int Count
        {
            get { return _count; }
        }

        #endregion

        public override string ToString()
        {
            return String.Format("{0} <{1}> [{2}]", GetType().Name, typeof(T).Name, string.Join(",", this));
        }
    }
}
